package com.atguigu.yygh.hosp.listener;

import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.rabbit.consts.MqConst;
import com.atguigu.yygh.rabbit.service.RabbitService;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//监听orderMqVo
//因为消息封装成orderMqVo所以就监听orderMqVo
//有消息的时候该方法执行
@Component
public class HospListener {
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    RabbitService rabbitService;

    //方法用于监听第一个队列,方法的参数必须和队列中的消息的类型一致
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            //队列和交换机不存在时就会自动创建,并且持久化
                            //当前服务监听的队列
                            value = @Queue(name = MqConst.ORDER_QUEUE,declare = "true",durable = "true"),
                            //该队列绑定到哪个交换机上
                            exchange = @Exchange(name = MqConst.ORDER_EXCHANGE,declare = "true",durable = "true"),
                            //队列和交换机的路由键
                            key = MqConst.ORDER_KEY
                    )
            }
    )
    //这个方法执行了,消息就从这个队列中移除了
    public void listenerOrderQueue(OrderMqVo orderMqVo){
        System.out.println("医院服务监听到了第一个队列中的消息");

        //监听到消息以后将消息更新到Mongodb中(更新Mongodb中的数据)
        String scheduleId = orderMqVo.getScheduleId();
        Integer availableNumber = orderMqVo.getAvailableNumber();
        Integer reservedNumber = orderMqVo.getReservedNumber();
        scheduleService.updateSchedule(scheduleId,reservedNumber,availableNumber);

        //2.向第二个交换机投递消息
        MsmVo msmVo = orderMqVo.getMsmVo();
        rabbitService.sendMessage(MqConst.MSM_EXCHANGE,MqConst.MSM_KEY,msmVo);
    }



    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            //队列和交换机不存在时就会自动创建,并且持久化
                            //当前服务监听的队列
                            value = @Queue(name = MqConst.CANCEL_ORDER_QUEUE,declare = "true",durable = "true"),
                            //该队列绑定到哪个交换机上
                            exchange = @Exchange(name = MqConst.CANCEL_ORDER_EXCHANGE,declare = "true",durable = "true"),
                            //队列和交换机的路由键
                            key = MqConst.CANCEL_ORDER_KEY
                    )
            }
    )
    //这个方法执行了,消息就从这个队列中移除了
    public void listenerCancelOrderQueue(OrderMqVo orderMqVo){
        String scheduleId = orderMqVo.getScheduleId();
        MsmVo msmVo = orderMqVo.getMsmVo();

        //1.将mongdb中排班的availableNumber+1
        scheduleService.addAvailableNum(scheduleId);

        //2.将msmVo发送到第二个队列上(短信服务不需要做任何改动)
        rabbitService.sendMessage(MqConst.MSM_EXCHANGE,MqConst.MSM_KEY,msmVo);
    }
}
